<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 下午 2:05
 */
namespace app\member\validate;
use app\member\model\Member;
use think\Db;
use think\Validate;

class ValidateFun extends Validate{
    public function __construct(array $rules = [], array $message = [], array $field = [])
    {
        parent::__construct($rules, $message, $field);
    }
    #验证用户名是否存在,存在，返回false
    public function checkUsername($v=''){
        $info = Db::name('member')->where(['del'=>1,'username'=>$v])->find();
        if ($info){
            return false;
        }else{
            return true;
        }
    }
    #验证手机号
    public function checkPhone($value=''){
        if(!preg_match("/^1[3-8]{1}\d{9}$/",$value)){
            return false;
        }else{
            return true;
        }
    }
    #验证手机号是否存在
    public function checkPhoneN($v=''){
        $info = Db::name('member')->where(['del'=>1,'phone'=>$v])->find();
        if ($info){
            return false;
        }else{
            return true;
        }
    }

    #验证密码
    public function checkPwd($v,$r,$data=[]){
        $login = new Member();
        $info = $login->mem_info($data);
        if ($info){
            $data['oldpwd']=getMd5($data['oldpwd'],$info['username'],$info['code']);
            if ($info['password'] == $data['oldpwd']){
                return true;
            }else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    #验证登录密码
    public function checkPwdLogin($v,$r,$data=[]){
        $info =Db::name('member')->where(['username'=>$data['username'],'del'=>1])->find();
        $data['password']=getMd5($data['password'],$data['username'],$info['code']);
        if ($info['password']==$data['password']){
            return true;
        }else{
            return false;
        }
    }
    #验证参数是否存在
    public function checkUserId($v=''){
        $info = Db::name('member')->where(['id'=>$v,'del'=>1])->find();
        if ($info){
            return true;
        }else{
            return false;
        }
    }
}